﻿using HystereticBasic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HystereticRegularization
{
    /// <summary>
    /// 任意曲线的双折线拟合
    /// </summary>
    public class RegularizedNormalCurve : CurveRegularizedBasic
    {
        /// <summary>
        /// 拐点
        /// </summary>
        private double Cx
        {
            get
            {
                return (this.PointJ.Disp - this.PointI.Disp) * this.afa + this.PointI.Disp;
            }
        }

        /// <summary>
        /// 拐点
        /// </summary>
        private double Cy
        {
            get
            {
                return (this.PointJ.Force - this.PointI.Force) * this.beta + this.PointI.Force;
            }
        }

        /// <summary>
        /// 计算线上的点值
        /// </summary>
        /// <param name="disp"></param>
        /// <returns></returns>
        public override double GetForceOnTangentLine(double disp)
        {
            //坐标是否落在AC点区间内
            if ((this.Cx - disp) * (this.PointI.Disp - disp) <= 0)
            {
                var slope = (this.Cy - this.PointI.Force) / (this.Cx - this.PointI.Disp);
                return slope * (disp - this.PointI.Disp) + this.PointI.Force;
            }
            else
            {
                var slope = (this.Cy - this.PointJ.Force) / (this.Cx - this.PointJ.Disp);
                return slope * (disp - this.PointJ.Disp) + this.PointJ.Force;
            }
        }

        /// <summary>
        /// 参数是否合法
        /// </summary>
        /// <returns></returns>
        public override bool isParamLegal()
        {
            //所有控制参数需为正 且不大1
            if (this.afa <= 0 || this.afa > 1)
                return false;
            if (this.beta <= 0 || this.beta > 1)
                return false;
            //合法
            return true;
        }

        /// <summary>
        /// 获得目标力位移曲线数据
        /// </summary>
        /// <returns></returns>
        public override MontForceDispObj GetTargetForceDispObj()
        {
            //初始化
            var obj = new MontForceDispObj();
            //添加初始点
            obj.Add(new ForceDispBasicPair(this.PointI));
            //添加拐点
            obj.Add(new ForceDispBasicPair((float)this.Cx, (float)this.Cy));
            //添加终点
            obj.Add(new ForceDispBasicPair(this.PointJ));
            return obj;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public RegularizedNormalCurve()
        {

        }

        /// <summary>
        /// 常用构造函数
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="beta"></param>
        public RegularizedNormalCurve(CureveRegularizedSOE forceDispObj, double afa, double beta)
            : base(forceDispObj, afa, beta)
        {
        }
    }
}